package com.top.ruizhong.main.mine

import android.graphics.Color
import android.view.Gravity
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import com.blankj.utilcode.util.BarUtils
import com.blankj.utilcode.util.ColorUtils
import com.google.android.material.appbar.AppBarLayout
import com.google.android.material.tabs.TabLayout
import com.module.lemlin.base.BaseAbstractFragment
import com.top.ruizhong.R
import com.yxing.ScanCodeActivity
import com.yxing.ScanCodeConfig
import com.yxing.def.ScanStyle
import kotlinx.android.synthetic.main.fragment_main_mine.*
import kotlinx.android.synthetic.main.include_mine_heads.*

class MainMineFragment : BaseAbstractFragment(), AppBarLayout.OnOffsetChangedListener {


    companion object {
        fun newInstance(): MainMineFragment {
            return MainMineFragment()
        }


        private val videoTabs: MutableList<String> = mutableListOf(
            "作品", "喜欢"
        )
    }

    override val layoutResId: Int
        get() = R.layout.fragment_main_mine


    override fun initView() {

        BarUtils.addMarginTopEqualStatusBarHeight(clMineBar)
        //为AppBar设置滑动监听
        appBarMineScroll.addOnOffsetChangedListener(this)


        //tab&vp
        tabMineVideo.apply {
            addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
                override fun onTabReselected(tab: TabLayout.Tab?) = Unit

                override fun onTabUnselected(tab: TabLayout.Tab?) {
                    tab?.customView = null
                }

                override fun onTabSelected(tab: TabLayout.Tab?) {
                    val text: TextView = TextView(context).apply {
                        setTextColor(ColorUtils.getColor(R.color.colorMain))
                        textSize = 16f
                        gravity = Gravity.CENTER
                        paint.isFakeBoldText = true
                    }
                    text.text = tab?.text
                    tab?.customView = text
                }
            })
        }.setupWithViewPager(vpMineBody.apply {
            adapter = object : FragmentStatePagerAdapter(
                childFragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT
            ) {

                override fun getItem(position: Int): Fragment =
                    MineVideoFragment.newInstance()

                override fun getCount(): Int = videoTabs.size

                override fun getPageTitle(position: Int): CharSequence? {
                    return videoTabs[position]
                }

                override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {}
            }
        })

        //编辑资料
        tvMineEditInfo.setOnClickListener {
            UserInfoActivity.open(activity = requireActivity())
        }
    }


    var alphaStatus: Int = 0

    private val alphaMaxHeight: Float =
        com.blankj.utilcode.util.ConvertUtils.dp2px(10f).toFloat()

    override fun onOffsetChanged(appBarLayout: AppBarLayout?, verticalOffset: Int) {

        val offset: Float = verticalOffset * -0.65f

        val alphaNewHeight: Float = alphaMaxHeight - (offset * 0.5f)

        var alpha2: Float =
            255 * (alphaMaxHeight - alphaNewHeight) / alphaMaxHeight
        if (alpha2 > 255)
            alpha2 = 255f
        clMineBar.setBackgroundColor(
            Color.argb(alpha2.toInt(), 255, 255, 255)
        )
        BarUtils.setStatusBarColor(
            requireActivity(), Color.argb(alpha2.toInt(), 255, 255, 255)
        )
        alphaStatus = alpha2.toInt()
    }

}